package Stock;

import java.util.Stack;

/**
 * @Classname JZ30min函数栈
 * @Description TODO
 * @Date 2023/2/24 18:59
 * @Created by xjl
 */
public class JZ30min函数栈 {

    /**
     * @description 最小栈的含义是每次从栈中获取的数据都是最小
      * @param: null
     * @date: 2023/2/24 19:00
     * @return:
     * @author: xjl
    */
    class MinStack {
        Stack<Integer> stack;

        /** initialize your data structure here. */
        public MinStack() {
            stack = new Stack<>();
        }
        /**
         * @description 每次都是push两个数据当前数据和当前最小的数据
          * @param: x
         * @date: 2023/2/24 19:01
         * @return: void
         * @author: xjl
        */
        public void push(int x) {
            if (stack.isEmpty()) {
                stack.add(x);
                stack.add(x);
            } else {
                int val=stack.peek();
                if (val > x) {
                    stack.add(x);
                    stack.add(x);
                } else {
                    stack.add(x);
                    stack.add(val);
                }
            }
        }
        /**
         * @description 每次都是弹出两个数据
          * @param:
         * @date: 2023/2/24 19:02
         * @return: void
         * @author: xjl
        */
        public void pop() {
            stack.pop();
            stack.pop();
        }

        /**
         * @description 获取顶部的元素，就是获取第二个元素
          * @param:
         * @date: 2023/2/24 19:02
         * @return: int
         * @author: xjl
        */
        public int top() {
            int min=stack.pop();
            int val=stack.pop();
            stack.push(val);
            stack.add(min);
            return val;
        }

        /**
         * @description 每次都是的获取最顶部的元素
          * @param:
         * @date: 2023/2/24 19:03
         * @return: int
         * @author: xjl
        */
        public int min() {
            return stack.peek();
        }
    }
}
